package com.sihan.swvp.action.sjfx;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.dao.sjfx.BzTjDao;

@Controller
@RequestMapping("/bztj")
public class BzTjAction extends BaseAction{
	
	@Autowired
	private BzTjDao dao;
	
	/**
	 * 班组今日任务统计 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getToDayTask")
	public Object getToDayTask(){
		List<Map<String, Object>> list = dao.getToDayTask();
		StringBuffer sb = new StringBuffer();
		sb.append("<chart basefontsize='11.5' labelDisplay='ROTATE' slantLabels='1' yAxisName='数量' caption='' numberPrefix='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
		if(!list.isEmpty()){
			for (int i = 0;i < list.size();i++) {
				sb.append("<set label='"+list.get(i).get("name")+"' value='"+list.get(i).get("counts")+"' />");
			}
		}else{
			sb.append("<set label='' value='' />");
		}
		sb.append("</chart>");
		
		return sb.toString();
	}
	
	/**
	 * 班组本周任务统计
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getWeekTask")
	public String getWeekTask(){
		List<Map<String, Object>> list = dao.getWeekTask();
		StringBuffer sb = new StringBuffer();
		sb.append("<chart basefontsize='11.5' labelDisplay='ROTATE' slantLabels='1' yAxisName='数量' caption='' numberPrefix='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
		if(!list.isEmpty()){
			for (int i = 0;i < list.size();i++) {
				sb.append("<set label='"+list.get(i).get("name")+"' value='"+list.get(i).get("counts")+"' />");
			}
		}else{
			sb.append("<set label='' value='' />");
		}
		sb.append("</chart>");
		
		return sb.toString();
	}
	
	
	/**
	 * 班组本周任务完成数量统计
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getWeekFinish")
	public Object getWeekFinish(){
		List<Map<String, Object>> list = dao.getWeekFinish();
		StringBuffer sb = new StringBuffer();
		sb.append("<chart caption='' formatNumberScale='0' basefontsize='11.5' showValues='0'>");
		for (int i = 0;i < list.size();i++) {
			Integer v = CommonUtil.o2int(list.get(i).get("counts"));
			String tip = list.get(i).get("name")+" , 完成任务："+v;
			String _tip = list.get(i).get("name")+" , 完成任务：0";
			if(null != v && v > 0){
				//sb.append("<set label='"+list.get(i).get("name")+","+v+"' value='"+v+"' toolText='"+tip+"'/>");
				sb.append("<set label='' value='"+v+"' toolText='"+tip+"'/>");
			}
			else{
				sb.append("<set label='' value='0.001' toolText='"+_tip+"'/>");
			}
			
		}
		if(list.isEmpty()){
			sb.append("<set label='' value='0.0001' toolText='完成任务：0'/>");
		}
		sb.append("</chart>");
		
		return sb;
	}
	
	/**
	 * 班组本周完成排名
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getWeekRanking")
	public Object getWeekRanking(){
		List<Map<String, Object>> list = dao.getWeekRanking();
		StringBuffer sb = new StringBuffer();
		sb.append("<chart basefontsize='11.5' labelDisplay='ROTATE' slantLabels='1' yAxisName='数量' caption='' numberPrefix='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
		for (int i = 0;i < list.size();i++) {
			sb.append("<set label='"+list.get(i).get("name")+"' value='"+list.get(i).get("counts")+"' />");
		}
		if(list.isEmpty()){
			sb.append("<set label='' value='' />");
		}
		sb.append("</chart>");
		
		return sb.toString();
	}
	
	/**
	 * 近30天班组完成排名
	 * @param oldt
	 * @param nowt
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getMonthRanking")
	public Object getMonthRanking(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		GregorianCalendar c = new GregorianCalendar();
	    c.add(Calendar.DAY_OF_MONTH, -30);
	    
		List<Map<String, Object>> list = dao.getMonthRanking(sdf.format(c.getTime()),sdf.format(new Date()));
		StringBuffer sb = new StringBuffer();
		sb.append("<chart basefontsize='11.5' labelDisplay='ROTATE' slantLabels='1' yAxisName='数量' caption='' numberPrefix='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
		for (int i = 0;i < list.size();i++) {
			sb.append("<set label='"+list.get(i).get("name")+"' value='"+list.get(i).get("counts")+"' />");
		}
		if(list.isEmpty()){
			sb.append("<set label='' value='' />");
		}
		sb.append("</chart>");
		
		return sb.toString();
	}
	
	
	@ResponseBody
	@RequestMapping("/getMonthFinishLv")
	public Object getMonthFinishLv(String bzname,String startTime ,String endTime){
		try {
			StringBuffer sb = new StringBuffer();
		    sb.append("<chart caption='' formatNumberScale='0' basefontsize='11.5' showValues='0'>");
		    List<Map<String, Object>> list = dao.getBzids(startTime,endTime,bzname);
		    if(null != bzname && !"".equals(bzname)){
		    	double getBzTaskSum = dao.getBzTaskSum(null, bzname,startTime, endTime);
	    		double getBzTaskFSum = dao.getBzTaskFSum(null,bzname, startTime, endTime);
	    		if(getBzTaskFSum <= 0){
	    			sb.append("<set label='' value = '0.00001' toolText='完成率：0'/>");
	    		}else{
	    			String d = new DecimalFormat("#.0").format(getBzTaskFSum/getBzTaskSum);
		    	    String lv = new DecimalFormat("#.0").format(getBzTaskFSum/getBzTaskSum*100);
		    	    sb.append("<set label='"+list.get(0).get("name")+"' value='"+d+"' toolText='"+list.get(0).get("name")+"，完成率："+lv+"%'/>");
	    		}
		    }else{
		    	if(!list.isEmpty()){
			    	for (int i = 0; i < list.size(); i++) {
			    		double getBzTaskSum = dao.getBzTaskSum(list.get(i).get("bzid").toString(), null,startTime, endTime);
			    		double getBzTaskFSum = dao.getBzTaskFSum(list.get(i).get("bzid").toString(), null,startTime, endTime);
			    	    String d = new DecimalFormat("#.0").format(getBzTaskFSum/getBzTaskSum);
			    	    String lv = new DecimalFormat("#.0").format(getBzTaskFSum/getBzTaskSum*100);
			    	    sb.append("<set label='"+list.get(i).get("name")+"' value='"+d+"' toolText='"+list.get(i).get("name")+"，完成率："+lv+"%'/>");
			    	    
					}
			    }else{
			    	sb.append("<set label='' value = '0.00001' toolText='完成率：0'/>");
			    }
		    }
		    sb.append("</chart>");
		    return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常");
		}
	    
	}
	
	
	/**
	 * 班组关联视频总量
	 * @param bzid 班组ID
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/bzVideoCounts")
	public Object bzVideoCounts(String bzname,String startTime ,String endTime){
		List<Map<String, Object>> list = dao.bzVideoCounts(bzname,startTime,endTime);
		StringBuffer sb = new StringBuffer();
		sb.append("<chart basefontsize='11.5' labelDisplay='ROTATE' slantLabels='1' yAxisName='数量' caption='' numberPrefix='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
		for (int i = 0;i < list.size();i++) {
			sb.append("<set label='"+list.get(i).get("name")+"' value='"+list.get(i).get("counts")+"' />");
		}
		if(list.isEmpty()){
			sb.append("<set label='' value='' />");
		}
		sb.append("</chart>");
		
		return sb.toString();
	}
	
	/**
	 * 班组任务数量统计
	 * @param bzid 班组ID
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/bzTaskCounts")
	public Object bzTaskCounts(String bzname,String startTime ,String endTime){
		List<Map<String, Object>> list = dao.bzVideoCounts(bzname,startTime,endTime);
		StringBuffer sb = new StringBuffer();
		sb.append("<chart basefontsize='11.5' labelDisplay='ROTATE' slantLabels='1' yAxisName='数量' caption='' numberPrefix='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
		for (int i = 0;i < list.size();i++) {
			sb.append("<set label='"+list.get(i).get("name")+"' value='"+list.get(i).get("counts")+"' />");
		}
		if(list.isEmpty()){
			sb.append("<set label='' value='' />");
		}
		sb.append("</chart>");
		
		return sb.toString();
	}
	
	/**
	 * 班组违规视频数据统计
	 * @param bzId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/bzWgVideoCounts")
	public Object bzWgVideoCounts(String bzname,String startTime ,String endTime){
		List<Map<String, Object>> list = dao.bzWgVideoCounts(bzname,startTime,endTime);
		StringBuffer sb = new StringBuffer();
		sb.append("<chart basefontsize='11.5' labelDisplay='ROTATE' slantLabels='1' canvasPadding='10' caption='' yAxisName='数量' bgColor='F7F7F7, E9E9E9' ");
		sb.append("numVDivLines='10' divLineAlpha='30'  labelPadding ='10' yAxisValuesPadding ='10' showValues='1'");
		sb.append("rotateValues='1' valuePosition='auto'>");
		sb.append("<categories>");
		if(!list.isEmpty()){
			for (int i = 0; i < list.size(); i++) {
				sb.append("<category label='"+list.get(i).get("name")+"' />");
			}
		}else{
			sb.append("<category label='' />");
		}
		sb.append("</categories>");
		sb.append("<dataset seriesName='' color='A66EDD' >");
		if(!list.isEmpty()){
			for (int i = 0; i < list.size(); i++) {
				sb.append("<set value='"+list.get(i).get("counts")+"' />");
			}
		}
		sb.append("</dataset>");
		sb.append("</chart>");
		
		return sb.toString();
	}
	

}
